<template>
    <div>
        <div style="display: flex;align-items: center;justify-content: center">
            <el-input style="width: 150px; margin: 0 10px" placeholder="查询竞赛标签" v-model="type" clearable></el-input>
            <el-button type="primary" @click="load(1)">查询</el-button>
            <el-button type="info" @click="reset" plain>重置</el-button>
        </div>
        <div style="margin: 10px 0">
            <el-button type="primary" plain @click="handleAdd">新增</el-button>
            <el-button type="danger" plain @click="delBatch">批量删除</el-button>
        </div>
        <el-table :data="tableData"
                  stripe
                  :header-cell-style="{ backgroundColor: 'aliceblue', color: '#666'}"
                  style="font-size: 14px"
                  @selection-change="handleSelectionChange"
        >
            <el-table-column type="selection" width="55" align="center"></el-table-column>
            <el-table-column prop="id" label="序号" width="70" align="center"></el-table-column>
            <el-table-column prop="addtime" label="创建时间" align="center"></el-table-column>
            <el-table-column prop="type" label="竞赛标签类型" align="center"></el-table-column>
            <el-table-column label="操作" align="center" width="180">
                <template v-slot="scope" style="font-size: 14px">
                    <el-button size="mini" type="primary" plain @click="handleEdit(scope.row)">编辑</el-button>
                    <el-button size="mini" type="danger" plain @click="del(scope.row.id)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <div style="margin: 10px 0; display: flex; align-items: center; justify-content: center">
            <el-pagination
                    @current-change="handleCurrentChange"
                    :current-page="pageNum"
                    :page-size="pageSize"
                    layout="total, prev, pager, next"
                    :total="total">
            </el-pagination>
        </div>
        <!--        弹窗-->
        <el-dialog title="竞赛标签配置" :visible.sync="fromVisible" width="30%">
            <el-form :model="form" label-width="80px" style="padding-right: 20px" :rules="rules" ref="formRef">
                <el-form-item label="竞赛标签" prop="type">
                    <el-input v-model="form.type"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="fromVisible = false">取 消</el-button>
                <el-button type="primary" @click="save">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "Sign",
        data() {
            return { 
                tableData:[], //所有的数据
                pageNum: 1, //当前的页码
                pageSize: 6, //每页显示的个数
                type: '',
                form: {},
                total: 0, //默认是0
                fromVisible: false,
                user: JSON.parse(localStorage.getItem("honey-user") || '{}'),//拿取个人用户信息
                rules: {
                    type: [
                        {required: true, message: '请输入竞赛标签', trigger: 'blur'}
                    ]
                },
                ids: []
            }
        },
        created() {
            this.load()
        },
        methods: {
            delBatch(){ //批量删除
                if (!this.ids.length){
                    this.$message.warning('请选择数据')
                    return
                }
                this.$confirm('确认批量删除这些数据吗？','确认删除',{type: "warning"}).then(response => {
                    this.$request.delete('/sign/delete/batch', {
                        data: this.ids
                    }).then(res => {
                        if (res.code === '200'){ //操作成功
                            this.$message.success('操作成功')
                            this.load(1) //刷新表格数据
                        }else { //操作失败
                            this.$message.error(res.msg) //弹出错误信息
                        }
                    })
                }).catch(() => {})
            },
            handleSelectionChange(rows){ //选中的数据 rows:当前选中的所有行数据
                this.ids = rows.map(v => v.id)
            },
            del(id){ //单个删除
                this.$confirm('确认删除？','确认删除',{type: "warning"}).then(response => {
                    this.$request.delete('/sign/delete/' + id).then(res => {
                        if (res.code === '200'){ //操作成功
                            this.$message.success('操作成功')
                            this.load(1) //刷新表格数据
                        }else { //操作失败
                            this.$message.error(res.msg) //弹出错误信息
                        }
                    })
                }).catch(() => {})
            },
            handleEdit(row){ //编辑
                this.form = JSON.parse(JSON.stringify(row)) //给form对象赋值 注意要深拷贝数据
                this.fromVisible = true //打开弹窗
            },
            handleAdd(){
                this.form = {} //新增数据的时候清空数据
                this.fromVisible = true //打开新增弹窗
            },
            save(){ //保存
                this.$refs.formRef.validate((valid) => {
                    if(valid){ //验证通过
                        this.$request({
                            url: this.form.id ? '/sign/update' : '/sign/add',
                            method: this.form.id ? 'PUT' : 'POST', //判断有无id,有为编辑,无为新增
                            data: this.form
                        }).then(res => {
                            if (res.code === '200'){ //成功保存
                                this.$message.success('保存成功')
                                this.load(1)
                                this.fromVisible = false
                            }else { //保存失败
                                this.$message.error(res.msg) //弹出错误信息
                            }
                        })

                    }
                })
            },
            reset(){
                this.type = ''
                this.load()
            },
            load(pageNum){ //分页查询
                if (pageNum){
                    this.pageNum = pageNum
                }
                this.$request.get('/sign/selectByPage', {
                    params: {
                        pageNum: this.pageNum,
                        pageSize: this.pageSize,
                        type: this.type,
                    }
                }).then(res => {
                    this.tableData = res.data.records
                    this.total = res.data.total
                })
            },
            handleCurrentChange(pageNum){
                this.load(pageNum)
            },
        }
    }

</script>

<style scoped>

</style>